﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using ZilLion.Wpf.Core.Print.Config;
using ZilLion.Wpf.Core.Print.DetailRow;

namespace CompentTest
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            #region 获取Config

            var config = new TemplateConfig
            {
                PirntType = PirntType.PivotPrint,
                DetailInfoDic = new Dictionary<string, string> {{"gsname", "货品名称"}, {"shopsendqty", "门店订货数量"}},
                PivotColConfigs = new List<PivotColConfig>
                {
                    new PivotColConfig
                    {
                        //FirstHeaderFieldName = "shoptype",
                        PivotColDataKey = "shopid",
                        PivotColKey = "shopsendqty",
                        PivotColValueKey = "qty"
                        //SecondHeaderFieldName = "qty"
                    }
                },
                RowGroupKey = "gsid",
                MaxColInPage = 3
            };

            #endregion

            #region 获取数据，然后获得PivotRange

            var printdata = PrintTest.GetTesTables();

            var detaildata = printdata["detaildata"];
            var headdata = printdata["headdata"];
            var headerdatalist = new List<DynamicHeaderEntity>();
            foreach (var row in headdata.Select())
                headerdatalist.Add(new DynamicHeaderEntity
                {
                    ColKey = row["shopid"].ToString(),
                    FirstHeader = row["shoptype"].ToString(),
                    SecondHeader = row["shopname"].ToString()
                });
            var pivotRangeCount = headerdatalist.Count / config.MaxColInPage +
                                  (headerdatalist.Count % config.MaxColInPage > 0 ? 1 : 0);


            var headerdatas = headerdatalist.OrderBy(x => x.FirstHeader).ToList();


            var ranges = new List<PivotRange>();

            for (var i = 0; i < pivotRangeCount; i++)
            {
                var range = new PivotRange();
                for (var j = 0; j < config.MaxColInPage; j++)
                {
                    var first = headerdatas.FirstOrDefault();
                    range.DynamicCols.Add(first);
                    if (first == null) continue;
                    range.DetailData.Add(first.ColKey,
                        detaildata.Select().Where(x => x["shopid"].ToString() == first.ColKey).ToArray());

                    headerdatas.Remove(first);
                }

                ranges.Add(range);
            }

            #endregion

            #region 读取模版，获得明细列头的初始位置 todo 从模版获取

            #region 固定列

            var gsnameheaderinfo = new DetailHeaderInfo {ColDataKey = "gsname", ColHederName = "货品", ColIndex = 0};

            var remarkheaderinfo = new DetailHeaderInfo
            {
                ColDataKey = "remark",
                ColHederName = "备注",
                ColIndex = 0 + config.MaxColInPage + 1
            };

            #endregion

            var rowkeys = detaildata.Select().Select(x => x[config.RowGroupKey].ToString()).Distinct();

            var fixedColData = rowkeys.ToDictionary(rowkey => rowkey, rowkey => detaildata.Select().FirstOrDefault(x => x[config.RowGroupKey].ToString() == rowkey));

            #endregion

            #region 填充数据

            #endregion
        }
    }

    /// <summary>
    ///     转至区域
    /// </summary>
    public class PivotRange
    {
        /// <summary>
        ///     动态列信息
        /// </summary>
        public IList<DynamicHeaderEntity> DynamicCols { get; set; } = new List<DynamicHeaderEntity>();

        /// <summary>
        ///     固定列数据
        /// </summary>
        public Dictionary<string, DataRow[]> DetailData { get; set; } = new Dictionary<string, DataRow[]>();
    }

    /// <summary>
    ///     动态列数据
    /// </summary>
    public class DynamicHeaderEntity
    {
        public string FirstHeader { get; set; }

        public string SecondHeader { get; set; }

        public string ColKey { get; set; }
    }
}